import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Res,
  Session,
  ParseIntPipe,
} from '@nestjs/common';
import { DemoService } from './demo.service';
import * as svgCaptcha from 'svg-captcha';
import { DemoPipe } from './pipe/demo.pipe'
import { CreateDemoDto } from './dto/create-demo.dto'

@Controller('demo')
export class DemoController {
  constructor(private readonly demoService: DemoService) {}

  @Post('pipe')
  execPipe(@Body() CreateDemoDto:CreateDemoDto){
    console.log("CreateDemoDto",CreateDemoDto)
  }

  @Get('list')
  getList() {
    return this.demoService.getList();
  }

  @Post('update')
  update(@Body() updateData) {
    return this.demoService.update();
  }

  @Post('agreement')
  getXieyiBody(@Body() body) {
    let agreement = body.agreement
      .replace(/\*\*\*\*\*/, '')
      .replace(/&&&&&/, '');
    const list = agreement.split('@');
    let address = Number(list[0])
      .toString(16)
      .toLocaleUpperCase()
      .padStart(2, '0');
    let stakeMark = list[1];
    let orderCode = list[2];
    let data = list[3];
    console.log(
      `地址:${address},桩号:${stakeMark},执行码:${orderCode},数据:${data}`,
    );
  }

  @Get('code')
  getSvgCode(@Session() session) {
    const c = svgCaptcha.create({
      size: 4,
      fontSize: 50,
      background: '#f00',
      width: 100,
      height: 50,
    });
    session.code = c.text.toLocaleUpperCase();
    // return {
    //   code: 200,
    //   data: c.data,
    //   message: 'ok',
    // };
    return c.data
  }

  @Post('login')
  createUser(@Body() body,@Session() session) {
    let code = body.code.toLocaleUpperCase()
    let sessionCode = session.code
    if(code == sessionCode){
      return {
        code:200,
        data:{},
        message:'ok'
      }
    }
    return {
      code:200,
      data:{},
      message:"code不对"
    }
  }

  @Get('service')
  demoHandler(){
    return this.demoService.createUser()
  }

  @Get(':id')
  testPipe(@Param('id',ParseIntPipe) id:number){
    console.log(typeof id)
  }
}
